package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.DictDao;
import com.atguigu.dao.HouseDao;
import com.atguigu.entity.House;
import com.atguigu.service.HouseService;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @author 杨林
 * @create 2022-09-18 17:06 星期日
 * description:
 */
@Service(interfaceClass = HouseService.class)
@Transactional
public class HouseServiceImpl extends BaseServiceImpl<House> implements HouseService {

    @Autowired
    private HouseDao houseDao;

    @Autowired
    private DictDao dictDao;

    @Override
    protected BaseDao<House> getEntityDao() {
        return houseDao;
    }

    @Override
    public House getById(Serializable id) {
        House house = super.getById(id);

        //houseTypeId --> houseTypeName
        String houseTypeName = dictDao.getNameById(house.getHouseTypeId());
        house.setHouseTypeName(houseTypeName);

        //floorId  --> floorName
        String floorName = dictDao.getNameById(house.getFloorId());
        house.setFloorName(floorName);

        //buildStructureId  -->  buildStructureName
        String buildStructureName = dictDao.getNameById(house.getBuildStructureId());
        house.setBuildStructureName(buildStructureName);

        //directionId  -->  directionName
        String directionName = dictDao.getNameById(house.getDirectionId());
        house.setDirectionName(directionName);

        //decorationId --> decorationName
        String decorationName = dictDao.getNameById(house.getDecorationId());
        house.setDecorationName(decorationName);

        //houseUseId  -->  houseUseName
        String houseUseName = dictDao.getNameById(house.getHouseUseId());
        house.setHouseUseName(houseUseName);

        return house;
    }

    @Override
    public PageInfo<HouseVo> finListPage(Integer pageNum, Integer pageSize, HouseQueryVo houseQueryVo) {

        //启用分页插件
        PageHelper.startPage(pageNum,pageSize);

        //调用dao层  获取分页数据
        Page<HouseVo> page =  this.houseDao.findListPage(houseQueryVo);

        List<HouseVo> houseVoList = page.getResult();

        for (HouseVo houseVo : houseVoList) {
            //houseTypeId --> houseTypeName
            String houseTypeName = dictDao.getNameById(houseVo.getHouseTypeId());
            houseVo.setHouseTypeName(houseTypeName);

            String floorName = dictDao.getNameById(houseVo.getFloorId());
            houseVo.setFloorName(floorName);

            String directionName = dictDao.getNameById(houseVo.getDirectionId());
            houseVo.setDirectionName(directionName);
        }



        return new PageInfo<HouseVo>(page.getResult());
        
        
    }
}
